package com.tl.boot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tl.boot.common.annotation.WithPermission;
import com.tl.boot.common.vo.PageInfoVO;
import com.tl.boot.common.vo.ResponseVO;
import com.tl.boot.entity.role.dto.AddRoleInfoDTO;
import com.tl.boot.entity.role.dto.ListRoleInfoDTO;
import com.tl.boot.entity.role.dto.UpdateRoleInfoDTO;
import com.tl.boot.entity.role.dto.UpdateRoleResBindInfoDTO;
import com.tl.boot.entity.role.form.*;
import com.tl.boot.entity.role.vo.ListRoleInfoVO;
import com.tl.boot.entity.role.vo.QueryRoleInfoVO;
import com.tl.boot.service.role.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * com.tl.boot.controller -- RoleController
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/16 11:07
 */
@RestController
@RequestMapping("auth/role")
@Api(tags = "角色模块")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("add")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation(value = "添加角色信息", notes = "传添加角色信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addRoleInfoForm", value = "添加角色信息请求对象", required = true,
                    dataType = "AddRoleInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO addRoleInfo(@RequestBody AddRoleInfoForm addRoleInfoForm) {
        AddRoleInfoDTO dto = new AddRoleInfoDTO();
        BeanUtils.copyProperties(addRoleInfoForm, dto);
        dto.setCreateTime(new Date(System.currentTimeMillis()));
        dto.setRoleStatus(addRoleInfoForm.getRoleStatusEnum().getCode().shortValue());
        Long roleId = roleService.addRoleInfo(dto);
        return ResponseVO.buildSuccess(roleId);
    }

    @PostMapping("delete")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation(value = "删除角色信息", notes = "传删除角色信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "删除角色信息请求对象", required = true,
                    dataType = "DelRoleInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO delRoleInfo(@RequestBody DelRoleInfoForm form) {
        roleService.delRoleInfo(form.getRoleId());
        return ResponseVO.buildSuccess();
    }

    @PostMapping("update")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation(value = "更新角色信息", notes = "传更新角色信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "更新角色信息请求对象", required = true,
                    dataType = "UpdateRoleInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO updateRoleInfo(@RequestBody UpdateRoleInfoForm form) {
        UpdateRoleInfoDTO dto = new UpdateRoleInfoDTO();
        BeanUtils.copyProperties(form, dto);
        dto.setRoleStatus(form.getRoleStatusEnum().getCode().shortValue());
        roleService.updateRoleInfo(dto);
        return ResponseVO.buildSuccess();
    }

    @PostMapping("updateRoleResBindInfo")
    @WithPermission(role = {"sysadmin"})
    @ApiOperation(value = "更新角色绑定权限信息信息", notes = "传更新角色绑定权限信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "更新角色绑定权限信息请求对象", required = true,
                    dataType = "UpdateRoleResourceBindInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO updateRoleResourceBindInfo(@RequestBody UpdateRoleResourceBindInfoForm form){
        UpdateRoleResBindInfoDTO dto = new UpdateRoleResBindInfoDTO();
        BeanUtils.copyProperties(form,dto);
        roleService.updateRoleResourceBindInfo(dto);
        return ResponseVO.buildSuccess();
    }


    @PostMapping("query")
    @ApiOperation(value = "查询角色详细信息", notes = "传查询角色详细信息请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "更新角色信息请求对象", required = true,
                    dataType = "QueryRoleInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO<QueryRoleInfoVO> queryRoleInfo(@RequestBody QueryRoleInfoForm form) {
        QueryRoleInfoVO queryRoleInfoVO = roleService.queryRoleInfo(form.getRoleId());
        return ResponseVO.buildSuccess(queryRoleInfoVO);
    }

    @PostMapping("list")
    @ApiOperation(value = "角色信息列表", notes = "传获取角色信息列表请求对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "listRoleInfoForm", value = "获取角色信息列表请求对象", required = true,
                    dataType = "ListRoleInfoForm", paramType = "body"),
            @ApiImplicitParam(name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header"),
    })
    public ResponseVO<PageInfoVO<ListRoleInfoVO>> listRoleInfo(@RequestBody ListRoleInfoForm listRoleInfoForm) {
        PageHelper.startPage(listRoleInfoForm.getPage(), listRoleInfoForm.getSize());
        ListRoleInfoDTO dto = new ListRoleInfoDTO();
        BeanUtils.copyProperties(listRoleInfoForm, dto);
        List<ListRoleInfoVO> roleInfoVOS = roleService.listRoleInfo(dto);
        Page<ListRoleInfoVO> page = (Page<ListRoleInfoVO>) roleInfoVOS;
        return ResponseVO.buildSuccess(page.getResult(), page.getTotal());
    }
}
